from pyecharts import options as opts
from pyecharts.charts import Bar
import pandas as pd

def toInt(x):
  return int(x)
def toFloat(x):
  return float(x)
def removeYear(x):
  return str(x)[5:]

def process(df):
  sheet = pd.DataFrame(df.loc[:,"天气状况"].value_counts())
  dict = sheet.iloc[:,0:1].to_dict()['天气状况']
  map = {}
  for key in dict:
    dayWeather = key.split('/')[0]
    if dayWeather in map:
      map[str(dayWeather)] += dict[key]
    else:
      map[str(dayWeather)] = dict[key]
  return map

def byWeather():
    # print(removeYear("2013-10-1"))
    excel = pd.ExcelFile(r'成都天气爬虫2014.xlsx') # 读取所有数据
    SheetNames = excel.sheet_names
    datas = [] # 各年天气数据
    dicts = [] # 各年天气字典
    tags = {} # 天气类型及编码
    tagIndex = 0
    tagArr = [] # 按编码排序的天气类型
    for i in SheetNames:
      sheet = pd.read_excel('成都天气爬虫2014.xlsx', sheet_name = i)
      df = pd.DataFrame(sheet)
      dict = process(df)
      dicts.append(dict)
      for key in dict:
        if not (key in tags):
          tags[key] = tagIndex
          tagIndex += 1
          tagArr.append(key)
      
    for dict_i in range (len(dicts)):
      datas.append([0]*tagIndex)
      for key in dicts[dict_i]:
        datas[dict_i][tags[key]] = dicts[dict_i][key]
    # _PM2p5(df)
    # _2021(df)

    # 绘图
    c = (
      Bar()
      .add_xaxis(tagArr)
      .add_yaxis("2014", datas[0], stack="stack1")
      .add_yaxis("2015", datas[1], stack="stack1")
      .add_yaxis("2016", datas[2], stack="stack1")
      .add_yaxis("2017", datas[3], stack="stack1")
      .add_yaxis("2018", datas[4], stack="stack1")
      .add_yaxis("2019", datas[5], stack="stack1")
      .add_yaxis("2020", datas[6], stack="stack1")
      .add_yaxis("2021", datas[7], stack="stack1")
      .render("bar_stack0.html")
    )

def byYear():
    # print(removeYear("2013-10-1"))
    excel = pd.ExcelFile(r'成都天气爬虫2014.xlsx') # 读取所有数据
    SheetNames = excel.sheet_names
    datas = [] # 各年天气数据
    dicts = [] # 各年天气字典
    tags = {} # 天气类型及编码
    tagIndex = 0
    tagArr = [] # 按编码排序的天气类型
    for i in SheetNames:
      sheet = pd.read_excel('成都天气爬虫2014.xlsx', sheet_name = i)
      df = pd.DataFrame(sheet)
      dict = process(df)
      dicts.append(dict)
      for key in dict:
        if not (key in tags):
          tags[key] = tagIndex
          tagIndex += 1
          tagArr.append(key)
    
    print(tags)

    for dict_i in range (len(dicts)):
      sum = 0
      datas.append([0]*tagIndex)
      for key in dicts[dict_i]:
        datas[dict_i][tags[key]] = dicts[dict_i][key]
        sum += dicts[dict_i][key]
      print(sum)
    # _PM2p5(df)
    # _2021(df)
    
    weatherData = []
    for key_i in range(len(tagArr)):
      weatherData.append([0]*14)
      for data_i in range(len(datas)):
        weatherData[key_i][data_i] = datas[data_i][key_i]


    # 绘图
    c = (
      Bar()
      .add_xaxis(["2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"])
      .add_yaxis("多云", weatherData[0], stack="stack1")
      .add_yaxis("阴", weatherData[1], stack="stack1")
      .add_yaxis("阵雨", weatherData[2], stack="stack1")
      .add_yaxis("小雨", weatherData[3], stack="stack1")
      .add_yaxis("晴", weatherData[4], stack="stack1")
      .add_yaxis("中雨", weatherData[5], stack="stack1")
      .add_yaxis("小到中雨", weatherData[6], stack="stack1")
      .add_yaxis("雨夹雪", weatherData[7], stack="stack1")
      .add_yaxis("大雨", weatherData[8], stack="stack1")
      .add_yaxis("大到中雨", weatherData[9], stack="stack1")
      .add_yaxis("暴雨", weatherData[10], stack="stack1")
      .add_yaxis("雷阵雨", weatherData[11], stack="stack1")
      .add_yaxis("雾", weatherData[12], stack="stack1")
      .render("bar_stack0_reverse_1.html")
    )
byWeather()